package com.ruoyi.web.controller.creditlife;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.creditlife.domain.CreditLifeBehavior;
import com.ruoyi.creditlife.domain.Parking;
import com.ruoyi.creditlife.service.ICreditLifeBehaviorService;
import com.ruoyi.creditlife.service.IParkingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * parkingController
 * 
 * @author ddd
 * @date 2025-06-30
 */
@RestController
@RequestMapping("/creditlife/parking")
public class ParkingController extends BaseController
{
    @Autowired
    private IParkingService parkingService;
    @Autowired
    private ICreditLifeBehaviorService creditLifeBehaviorService;
    /**
     * 查询parking列表
     */
    @PreAuthorize("@ss.hasPermi('creditlife:parking')")
    @GetMapping("/list")
    public TableDataInfo list(Parking parking)
    {

        startPage();
        List<Parking> list = parkingService.selectParkingList(parking);
        return getDataTable(list);
    }

    /**
     * 导出parking列表
     */
    @PreAuthorize("@ss.hasPermi('creditlife:parking')")
    @Log(title = "parking", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Parking parking)
    {
        List<Parking> list = parkingService.selectParkingList(parking);
        ExcelUtil<Parking> util = new ExcelUtil<Parking>(Parking.class);
        util.exportExcel(response, list, "parking数据");
    }

    /**
     * 获取parking详细信息
     */
    @PreAuthorize("@ss.hasPermi('creditlife:parking')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(parkingService.selectParkingById(id));
    }

    /**
     * 新增parking
     */
    @PreAuthorize("@ss.hasPermi('creditlife:parking')")
    @Log(title = "parking", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Parking parking)
    {
        return toAjax(parkingService.insertParking(parking));
    }

    /**
     * 修改parking
     */
//    @PreAuthorize("@ss.hasPermi('creditlife:parking:edit')")
//    @Log(title = "parking", businessType = BusinessType.UPDATE)
//    @PutMapping
//    public AjaxResult edit(@RequestBody Parking parking)
//    {
//        CreditLifeBehavior behavior  = new CreditLifeBehavior();
//        return toAjax(parkingService.updateParking(parking));
//    }
    @PreAuthorize("@ss.hasPermi('creditlife:parking')")
    @Log(title = "parking", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Parking parking, HttpServletRequest request)
    {
        // 获取当前登录用户
        //判断信用分是否达到要求
        LoginUser loginUser = getLoginUser();
        Long userId = null;
        // 检查用户是否登录
        if (loginUser != null) {
            userId = loginUser.getUserId(); // 假设LoginUser类中有getUserId()方法
        } else {
            // 处理未登录情况，根据业务需求可以选择抛出异常或记录系统用户
            throw new RuntimeException("用户未登录");

        }
        // 创建行为记录
        CreditLifeBehavior behavior = new CreditLifeBehavior();
        behavior.setUserId(userId); // 设置用户ID
        behavior.setServiceType((long) 1); // 设置服务类型
        behavior.setBehaviorType("便捷泊车"); // 设置行为类型
        behavior.setStartTime(new Date()); // 设置行为时间
        behavior.setAmount(new BigDecimal(parking.getPrice()));
        behavior.setLocation(parking.getParkAddress()); // 设置行为地点
        behavior.setStatus("进行中"); // 设置行为金额
//        behavior.setAmount(); // 设置行为金额
//        behaviorService.insertBehavior(behavior); // 假设存在此服务方法
        // 更新停车场信息
        return toAjax(creditLifeBehaviorService.insertCreditLifeBehavior(behavior));
    }

    /**
     * 删除parking
     */
    @PreAuthorize("@ss.hasPermi('creditlife:parking')")
    @Log(title = "parking", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(parkingService.deleteParkingByIds(ids));
    }
}
